<!DOCTYPE html>
<html lang="en">

<head>
    <title>常见电信网络诈骗防范知识测验</title>
    <#include "/dt/components/meta.html" />
    <#include "/dt/components/stylesheets.html" />
    <link rel="stylesheet" href="/dt/stylesheets/index.css?v=8">
</head>

<body>
    <div id="app">
        <div hidden>
            <img src="https://i.loli.net/2018/09/16/5b9de3f1c5ebf.jpg">
        </div>
        <div class="music" @click="playOrPause">
            <i class="am-icon-music am-icon-spin" id="music-icon"></i>
        </div>
        <div class="am-g am-g-collapse top">
            <div class="am-u-sm-6 logo cutimg">
                <img src="https://i.loli.net/2018/09/14/5b9b8f08e69b0.png" />
            </div>
            <div class="am-u-sm-6 info">
                <div class="info-container">
                    <img src="https://i.loli.net/2018/09/14/5b9b8f08c3087.png" />
                    <span>请点击正确选项（以下10题均为多项选择题），每题10分，总分100分，共10题。</span>
                </div>
            </div>
        </div>
        <div class="question-container">
            <div class="wrap">
                <div class="header theme-background-color1">
                    <span>第{{index+1}}题</span>
                </div>
                <div class="body">
                    <div class="content" v-if="questions.length > 0">
                        <span>{{ questions[index].content }}</span>
                    </div>
                    <div class="answers" v-if="questions.length > 0">
                        <div class="am-form-group">
                            <div class="answers-item" v-for="item in questions[index].items" :key="item.id">
                                <label class="am-checkbox-inline">
                                    <input v-if='item.isChecked === 0' type="checkbox" value="item.id" @click="select(item.id,index)">{{ item.content }}
                                    <input v-else type="checkbox" value="item.id" @click="select(item.id,index)" checked>{{ item.content }}
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="am-g buttons">
            <div class="am-u-sm-6">
                <button class="am-btn am-btn-default theme-background-color1 theme-border-color1" @click="prevQuestion" v-if="index > 0">上一题</button>
            </div>
            <div class="am-u-sm-6">
                <button class="am-btn am-btn-default theme-background-color1 theme-border-color1" @click="nextQuestion" v-if="index < questions.length - 1">下一题</button>
                <button class="am-btn am-btn-default theme-background-color1 theme-border-color1" @click="submit" v-else>提交</button>
            </div>
        </div>
        <div class="footer">
            <img src="https://i.loli.net/2018/09/14/5b9b8f08d4f8c.png" />
        </div>
        <div class="copyright">
            <a href="http://www.zhen-yee.com/">  </a>
        </div>
        <div class="am-modal am-modal-no-btn modal" tabindex="-1" id="doc-modal-1">
            <div class="am-modal-dialog dialog">
                <div class="am-modal-hd">
                </div>
                <div class="am-modal-bd body theme-background-color1">
                    <div class="dialog-top">答题未全对</div>
                    <div class="dialog-info">很遗憾，没有机会抽奖</div>
                    <div class="dialog-tip">（今天还有 ${(chance-1)!} 次答题机会）</div>
                    <div class="am-g am-g-collapse dialog-buttons">
                        <div class="am-u-sm-6 giveup">
                            <button class="am-btn am-btn-default theme-color1" @click="giveup">放弃</button>
                        </div>
                        <div class="am-u-sm-6 restart">
                            <button class="am-btn am-btn-default theme-color1" @click="restart" v-if="true">重新答题</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <audio src="http://res.zhen-yee.com/temp/re：plus%20-%20Everlasting%20Truth.mp3" id="Jaudio" class="media-audio" autoplay preload loop="loop"></audio>
    <#include "/dt/components/scripts.html" />
    <#include "/dt/components/wxConfig.html" />
    <script type="text/javascript">
    function contains(array, item) {
        for (var i = 0; i < array.length; i++) {
            if (array[i] == item) {
                return i;
            }
        }
        return -1;
    }
    var app = new Vue({
        el: '#app',
        data() {
            return {
                index: 0,
                questions: [],
                answers: [],
                currentAnswer: []
            }
        },
        mounted: function() {
            this.init();
        },
        methods: {
            init() {
                var self = this;
                var obj = JSON.parse('${questions!}');
                self.questions = obj;
                self.index = 0;
                var ql = self.questions.length
                for (var qidx = 0; qidx < ql; qidx++) {
                    for (var idx = 0; idx < 4; idx++) {
                        self.questions[qidx].items[idx]["isChecked"] = 0
                    }
                }
                // for()
            },
            select(val, index) {
                var self = this;
                var question = self.questions[index]
                var len = question.items.length
                for (var idx = 0; idx < len; idx++) {
                    var item = question.items[idx]
                    if (item.id == val) {
                        var isChecked = self.questions[index].items[idx]["isChecked"]
                        isChecked = (isChecked + 1) % 2;
                        self.questions[index].items[idx]["isChecked"] = isChecked
                    }
                }
            },
            nextQuestion() {
                var self = this;
                if (self.index >= self.questions.length) {
                    return;
                }
                self.index++;
            },
            prevQuestion() {
                var self = this;
                if (self.index <= 0) {
                    return;
                }
                self.index--;
            },
            submit() {
                var self = this;
                var result = [];
                loading();

                var ql = self.questions.length
                for (var qidx = 0; qidx < ql; qidx++) {
                    var question = self.questions[qidx]
                    var items = ""
                    for (var idx = 0; idx < question.items.length; idx++) {
                        var item = question.items[idx]
                        if (item.isChecked == 1) {
                            items = items + item.id + ";"
                        }
                    }
                    var questionItem = {
                        questionId: question.id,
                        items: items
                    }
                    result.push(questionItem);
                }
                var param = Qs.stringify({
                    answer: JSON.stringify(result)
                });
                axios({
                        url: '/dt/submit',
                        method: 'post',
                        data: param,
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        }
                    })
                    .then(function(response) {
                        endLoading();
                        if (response.data.status == 200) {
                            window.location.href = "/dt/draw";
                        } else {
                            var $modal = $('#doc-modal-1');
                            $modal.modal({
                                closeViaDimmer: false
                            });
                        }
                    })
                    .catch(function(error) {
                        endLoading();
                    });
            },
            giveup() {
                wx.closeWindow();
            },
            restart() {
                window.location.href = "/dt";
            },
            playOrPause() {
                var audio = document.getElementById('Jaudio');
                var icon = $('#music-icon');
                if (audio.paused) {
                    audio.play();
                    icon.addClass('am-icon-spin');
                } else {
                    audio.pause();
                    icon.removeClass('am-icon-spin');
                }
            }
        }
    })
    </script>
    <script>
    function audioAutoPlay(id) {
        var audio = document.getElementById(id);
        audio.play();
        document.addEventListener("WeixinJSBridgeReady", function() {
            audio.play();
        }, false);
        document.addEventListener('YixinJSBridgeReady', function() {
            audio.play();
        }, false);
    }
    wx.ready(function() {
        initWXShare()
        audioAutoPlay('Jaudio');
    })
    </script>
</body>

</html>